package com.Arsanesia.TempleRush.Models;

import com.Arsanesia.TempleRush.Views.Screens.AbstractScreen;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;

public class Smoke extends Image implements Updateable{
	private final float smokeSpeed;
	private final float maxXDestination;
	private final float minXDestination;

	
	private Image SmokeBg;
	private Image smoke1;
	private Image smoke2;
	private Action disappearaction;
	private Action appearaction;
	
	public Smoke(TextureAtlas atlas){
		//create smokes;
		SmokeBg = new Image(new TextureRegionDrawable(new TextureRegion(atlas.createSprite("smokebg"))));
		smoke1 = new Image(new TextureRegionDrawable(new TextureRegion(atlas.createSprite("smoke1"))));
		smoke2 = new Image(new TextureRegionDrawable(new TextureRegion(atlas.createSprite("smoke2"))));
		//set to transparent first
		SmokeBg.addAction(Actions.alpha(0));
		smoke1.addAction(Actions.alpha(0));
		smoke2.addAction(Actions.alpha(0));
		maxXDestination = AbstractScreen.VIEWPORT_DEFAULTWIDTH + smoke1.getWidth();
		minXDestination = 0 - smoke1.getWidth();

		smokeSpeed = 3f;
	    disappearaction = Actions.alpha(0,0.2f);
	    appearaction = Actions.alpha(1,0.5f);
	}
	
	public Image getSmokeBg(){
		return SmokeBg;
	}
	
	public void addSmokeBackgroundToStage(Stage stage){
		stage.addActor(SmokeBg);
	}
	public void addSmokeToStage(Stage stage){
		stage.addActor(smoke1);
		stage.addActor(smoke2);
	}
	
	public void startSmoke(int line){
		//get the smoke on the line
		SmokeBg.setY(line-SmokeBg.getHeight());
		smoke1.setY(line-SmokeBg.getHeight());
		smoke1.setX(minXDestination);
		smoke2.setY(line-SmokeBg.getHeight());
		smoke2.setX(maxXDestination);
		//animate smoke
		SmokeBg.addAction(Actions.alpha(1,0.5f));
		smoke1.addAction(Actions.alpha(1,0.5f));
		smoke2.addAction(Actions.alpha(1,0.5f));
		
	}

	@Override
	public void update() {
		smoke1.addAction(Actions.moveBy(smokeSpeed, 0,0.1f));
		smoke2.addAction(Actions.moveBy(-smokeSpeed, 0,0.1f));
		
		if(smoke1.getX()>=maxXDestination)disappear();
		
	}
	
	private void disappear(){
		SmokeBg.addAction(Actions.alpha(0,0.2f));
		smoke1.addAction(Actions.alpha(0,0.2f));
		smoke2.addAction(Actions.alpha(0,0.2f));
	}

}
